//
//  YSaEmotionToolBarView.swift
//  新浪浪
//
//  Created by mac on 16/3/10.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

enum YSaEmotionToolBarType: Int {
    case Recent = 10000
    case Default = 10001
    case Emoji = 10002
    case Lxh = 10003
}

protocol YSaEmotionToolBarViewDelegate: NSObjectProtocol {
    func didEmotionToolBarItemClick(type:YSaEmotionToolBarType)
}

class YSaEmotionToolBarView: UIView {

    weak var delegate : YSaEmotionToolBarViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(recentBtn)
        addSubview(defaultBtn)
        addSubview(emojiBtn)
        addSubview(lxhBtn)
        
        recentBtn.snp_makeConstraints { (make) -> Void in //上下左宽
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            make.left.equalTo(self.snp_left)
            make.width.equalTo(defaultBtn.snp_width)
        }
        
        defaultBtn.snp_makeConstraints { (make) -> Void in //上下左宽
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            make.left.equalTo(recentBtn.snp_right)
            make.width.equalTo(emojiBtn.snp_width)
        }
        
        emojiBtn.snp_makeConstraints { (make) -> Void in //上下左宽
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            make.left.equalTo(defaultBtn.snp_right)
            make.width.equalTo(lxhBtn.snp_width)
        }
        
        lxhBtn.snp_makeConstraints { (make) -> Void in //上下左右
            make.top.equalTo(self.snp_top)
            make.bottom.equalTo(self.snp_bottom)
            make.left.equalTo(emojiBtn.snp_right)
            make.right.equalTo(self.snp_right)
        }
        
    }
    
    @objc private func changeBtnEmotion(button:UIButton){
        for subview in subviews {
            let button = subview as! UIButton
            button.selected = false
        }
        button.selected = true
        delegate?.didEmotionToolBarItemClick(YSaEmotionToolBarType (rawValue: button.tag)!)
    }
    
    //懒加载recent default emoji lxh
    private lazy var recentBtn : UIButton = {
        
        let btn = UIButton (type: UIButtonType.Custom)
        btn.setBackgroundImage (UIImage (named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage (named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        btn.setTitle("最近", forState: UIControlState.Normal)
        btn.addTarget(self, action: "changeBtnEmotion:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.tag = YSaEmotionToolBarType.Recent.rawValue
        return btn
        
        }()
    
    private lazy var defaultBtn : UIButton = {
        
        let btn = UIButton (type: UIButtonType.Custom)
        btn.setBackgroundImage (UIImage (named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage (named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        btn.setTitle("默认", forState: UIControlState.Normal)
        btn.addTarget(self, action: "changeBtnEmotion:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.tag = YSaEmotionToolBarType.Default.rawValue
        return btn
        
        }()
    
    private lazy var emojiBtn : UIButton = {
        
        let btn = UIButton (type: UIButtonType.Custom)
        btn.setBackgroundImage (UIImage (named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage (named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        btn.setTitle("emoji", forState: UIControlState.Normal)
        btn.addTarget(self, action: "changeBtnEmotion:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.tag = YSaEmotionToolBarType.Emoji.rawValue
        return btn
        
        }()
    
    private lazy var lxhBtn : UIButton = {
        
        let btn = UIButton (type: UIButtonType.Custom)
        btn.setBackgroundImage (UIImage (named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage (named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        btn.setTitle("浪小花", forState: UIControlState.Normal)
        btn.addTarget(self, action: "changeBtnEmotion:", forControlEvents: UIControlEvents.TouchUpInside)
        btn.tag = YSaEmotionToolBarType.Lxh.rawValue
        return btn
        
        }()
    
    
}
